// 每日统计API
import { NextRequest } from 'next/server';
import { StatsDB } from '@/lib/mysql';
import { getCurrentUser, ApiResponse } from '@/lib/auth';

// 获取用户每日统计
export async function GET(request: NextRequest) {
  try {
    // 验证用户登录
    const currentUser = getCurrentUser();
    if (!currentUser) {
      return ApiResponse.unauthorized('请先登录');
    }

    // 获取查询参数
    const { searchParams } = new URL(request.url);
    const daysParam = searchParams.get('days');
    const days = daysParam ? parseInt(daysParam) : 7;

    // 获取每日统计
    const dailyStats = await StatsDB.getDailyStats(currentUser.userId, days);

    return ApiResponse.success({
      stats: dailyStats,
      days,
      total: dailyStats.length,
    });
  } catch (error) {
    console.error('获取每日统计错误:', error);
    return ApiResponse.serverError('获取每日统计失败');
  }
}
